package com.itheima.d3_collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 需求：总共有54张牌
 * 点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2“
 * 花色: "♠", "♥", "♣", "♦"
 * 大小王: "👲" , "🃏“
 * 斗地主：发出51张牌，剩下3张做为底牌。
 */
public class Demo3 {
    public static void main(String[] args) {
        //1.制作牌堆，拥有54张牌，制作好之后，要洗牌（随机打乱牌的顺序）
        //1.1 创建ArrayList集合，存储54张扑克牌。泛型声明为：Poker类型。将每一张牌制作成Poker对象！
        ArrayList<Poker> list = new ArrayList<>();

        //1.2 遍历点数，再遍历花色，然后创建Poker对象，存入集合（注意：扑克对象还有id属性）
        int id = 1;
        String[] dianArr = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] huaArr = {"♠", "♥", "♣", "♦"};
        for (String dian : dianArr) {
            for (String hua : huaArr) {
                //创建Poker对象，封装点数、花色、id(id的值越来越大)
                Poker poker = new Poker(hua, dian, id++);
                //存入集合
                list.add(poker);
            }
        }
        //存入大小王
        list.add(new Poker("👲", "", id++));
        list.add(new Poker("🃏", "", id++));
        //1.3 洗牌
        Collections.shuffle(list);
        System.out.println("洗牌后，牌堆：" + list);
        System.out.println("--------------------------");

        //2.分别给3个人发牌，这里是3个集合，每个集合存储一些扑克牌
        //2.1 创建3个集合，用来存储3个人牌
        ArrayList<Poker> zs = new ArrayList<>();
        ArrayList<Poker> ls = new ArrayList<>();
        ArrayList<Poker> ww = new ArrayList<>();

        //2.2 分别给三个集合发牌！（前17张给zs发，中间17张给ls发，最后20张给ww发）
        for (int i = 0; i <= 16; i++) {
            Poker poker = list.get(i);
            zs.add(poker);
        }

        for (int i = 17; i <= 33; i++) {
            ls.add(list.get(i));
        }

        for (int i = 34; i < list.size(); i++) {
            ww.add(list.get(i));
        }

        //3.对3个集合的牌按照斗地主的规则进行排序，然后打印牌
       //3.1 制作一个Comparator比较器，根据Poker对象的id属性进行升序排序
        Comparator<Poker> comparator = new Comparator<Poker>() {
            @Override
            public int compare(Poker o1, Poker o2) {
                //根据id升序排序
                int id1 = o1.getId();
                int id2 = o2.getId();
                return id1 - id2;
            }
        };

        //3.2 分别对3个集合进行排序
        Collections.sort(zs, comparator);
        Collections.sort(ls, comparator);
        Collections.sort(ww, comparator);

        System.out.println("张三：" + zs);
        System.out.println("李四：" + ls);
        System.out.println("王五：" + ww);
    }
}
